//
//  CM_NavigationViewController.m
//  C2CMember
//
//  Created by yangy on 2018/7/2.
//  Copyright © 2018年 iphone. All rights reserved.
//

#import "CM_NavigationViewController.h"
#import "RDVTabBarController.h"

@interface CM_NavigationViewController ()<UINavigationControllerDelegate>
@property (strong, nonatomic)  UIPanGestureRecognizer *pan;
@end

@implementation CM_NavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.禁用系统自带的返回手势
    self.interactivePopGestureRecognizer.enabled = NO;
    //2.捕获返回手势的代理
    //id target = self.interactivePopGestureRecognizer.delegate;
    // 3.添加全屏滑动返回手势，事件指向系统自带的Pop转场
    //SEL backGestureSelector = NSSelectorFromString(@"handleNavigationTransition:");
    //self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:backGestureSelector];
    //4.添加手势
    //[self.view addGestureRecognizer:self.pan];
    
    //5.设置自己为自己代理，这样的设置不科学，但可用
    self.delegate = self;
    
    //self.navigationBar.barTintColor = cmColorFFFFFF;
    self.navigationBar.translucent = NO;
    
    self.navigationBar.shadowImage = [UIImage new];
}


//捕获push事件，定义全局返回按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    // 判断下当前控制器是不是根控制器
    if (self.viewControllers.count > 0) {
        // 设置导航条左上角按钮
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.frame = CGRectMake(0, 0, 44, 44);
        [leftButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
        leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, -16,0, 0);
        leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -20,0, 0);
        [leftButton setEnlargeEdgeWithTop:0 right:10 bottom:10 left:40];
        [[leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self popViewControllerAnimated:YES];
        }];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        viewController.navigationItem.leftBarButtonItem = item;
    }
    // 真正跳转
    [super pushViewController:viewController animated:animated];
}

//#pragma mark - UINavigationControllerDelegate
////导航控制器的代理，处理Pan手势是否生效，解决NavitionController的根控制器转场错乱
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    // 判断下当前控制器是不是根控制器
    
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (navigationController.viewControllers.count>1) {
        RDVTabBarController *tabbarVC = (RDVTabBarController*)kAppDelegate.window.rootViewController;
        [tabbarVC setTabBarHidden:YES animated:YES];
    }else{
        //self.pan.enabled = NO;//手势无效，无法转场，自然不会出现转场错乱的问题。
        RDVTabBarController *tabbarVC = (RDVTabBarController*)kAppDelegate.window.rootViewController;
        [tabbarVC setTabBarHidden:NO animated:YES];
    }
}


@end
